package com.central.scheduler.dao.handler;

import com.central.common.model.WorkerServer;
import com.central.scheduler.common.utils.JSONUtils;
import com.central.scheduler.dao.entity.TaskNodeParam;
import com.nimbusds.jose.shaded.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Tindy
 * @date 2021/6/23
 * @describe
 */
@MappedJdbcTypes(JdbcType.VARCHAR)  // 数据库类型
@MappedTypes({List.class})          // java数据类型
public class ListTaskNodeParamToStringHandler extends BaseTypeHandler<List<TaskNodeParam>> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List list, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSONUtils.toJsonString(list));
    }

    @Override
    public List getNullableResult(ResultSet resultSet, String s) throws SQLException {
        List jsonArray = JSONUtils.toList(resultSet.getString(s),TaskNodeParam.class);
        return jsonArray;
    }

    @Override
    public List getNullableResult(ResultSet resultSet, int i) throws SQLException {
        List jsonArray = JSONUtils.toList(resultSet.getString(i),TaskNodeParam.class);
        return jsonArray;
    }

    @Override
    public List getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        List jsonArray = JSONUtils.toList(callableStatement.getString(i),TaskNodeParam.class);
        return jsonArray;
    }
}

